# coding: utf-8

# 将企业推广首页和sem推广使用的虚拟号，设置为 reserved = True
# 将相关虚拟号的 VirtualNumber 记录设置为 reserved = True

import os
import json
import time
import datetime

import set_env_path     # 请勿删除
from sqlalchemy import create_engine
from sqlalchemy.sql import text


engine = create_engine(
    os.getenv('DATABASE_URL')
    or 'mysql+pymysql://root:@127.0.0.1/wph_www_prod?charset=utf8'
)
conn = engine.connect()


def main():
    """
    add by zhouhl 2018-06-01
    """
    # 查询所有企业推广首页在用的虚拟号
    get_enterprise_number_sql = text("""
        select id from number_pool_for_lt where enterprise_id<>0;
    """)

    # 查询sem使用的虚拟号
    get_sem_number_sql = text("""
        select id from number_pool_for_lt where tel in ('18618207054', '18618226248', '18618226842');
    """)

    update_reserved_sql = text("""
        update number_pool_for_lt set reserved=true where id in :ids;
    """)

    ids_1 = [i.id for i in conn.execute(get_enterprise_number_sql).fetchall()]
    ids_2 = [i.id for i in conn.execute(get_sem_number_sql).fetchall()]
    ids = ids_1 + ids_2

    conn.execute(
        update_reserved_sql,
        ids=ids
    )

    update_virtual_numbers_sql = text("""
        update virtual_numbers set reserved=true where tel_b='' and tel_x in ('18618207054', '18618226248', '18618226842') and valid=1;
    """)

    conn.execute(update_virtual_numbers_sql)


if __name__ == '__main__':
    main()
